<template>
    <div>
        <el-dialog :title="form.id ? '修改用户信息' : '添加用户信息'" :visible.sync="dialogVisible" width="50%"
            @close="resetForm('refName')" :close-on-click-modal="false">
            <!-- 内容主题区 -->
            <el-form ref="refName" :model="form" :rules="formRules" label-width="70px">
                <el-form-item label="用户名称" prop="username">
                    <el-input v-model="form.username" :disabled="isDisabled"></el-input>
                </el-form-item>
                <el-form-item label="用户密码" prop="password" v-if="!form.id">
                    <el-input v-model="form.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="form.mobile"></el-input>
                </el-form-item>
            </el-form>
            <!-- 底部 -->
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button v-if="!form.id" @click="resetForm('refName')">重 置</el-button>
                <el-button type="primary" @click="submitForm">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        // 验证邮箱
        var checkEmail = (rule, value, callback) => {
            // 邮箱验证的正则表达式
            const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
            if (regEmail.test(value))
                return callback() // 合法邮箱
            callback(new Error('请输入合法邮箱'))
        }
        // 手机号验证
        var checkMobile = (rule, value, callback) => {
            // 手机号验证的正则表达式
            const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
            if (regMobile.test(value))
                return callback() // 合法手机号
            callback(new Error('请输入合法手机号'))
        }
        return {
            form: {
                username: '',
                password: '',
                email: '',
                mobile: '',
            },
            dialogVisible: false,
            isDisabled: false, // 控制用户名称输入框是否可编辑
            // 表单验证规则
            formRules: {
                username: [
                    { required: true, message: '请输入用户名称', trigger: 'blur' },
                    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { validator: checkEmail, trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { validator: checkMobile, trigger: 'blur' }
                ],
            },
        };
    },
    methods: {
        initData(value) { // 初始化对话框
            this.isDisabled = false
            if (value && value.id) {
                this.form = JSON.parse(JSON.stringify(value)) // 深拷贝，避免共用一个地址
                this.isDisabled = true
            }
            this.dialogVisible = true
            console.log('初始化数据：' + JSON.stringify(this.form))
        },
        resetForm(refName) { // 重置表单信息
            this.form = {
                username: '',
                password: '',
                email: '',
                mobile: '',
            }
            this.$refs[refName].resetFields()
        },
        submitForm() { // 提交用户信息
            this.$refs.refName.validate(async valid => {
                if (!valid) return
                if (this.form && this.form.id) { // 修改用户信息
                    const res = await this.$http.put(`users/${this.form.id}`, this.form)
                    console.log(res)
                    if (res.meta.status != 200) return this.$message.error('更新用户信息失败')
                    this.$message.success('更新用户信息成功')
                    this.dialogVisible = false
                    this.$parent.getUserList()
                } else { // 添加用户信息操作
                    const res = await this.$http.post('users', this.form)
                    console.log(res)
                    if (res.meta.status != 201) return this.$message.error('添加用户信息失败')
                    this.$message.success('添加用户信息成功')
                    this.dialogVisible = false
                    this.$parent.getUserList() // 子组件调用父组件方法
                }
            })
        },
    },
};
</script>

<style lang="less" scoped>

</style>